package com.gko100.dao.impl;

import java.util.List;

import com.gko100.dao.ImageDao;
import com.gko100.dao.basehibernatesupport.BaseHibernateDaoSupport;
import com.gko100.model.Administrator;
import com.gko100.model.Article;
import com.gko100.model.Category;
import com.gko100.model.Image;
import com.gko100.model.Navigation;

public class ImageDaoHibernate extends BaseHibernateDaoSupport implements ImageDao {

	public void delete(Image image) {
		this.getHibernateTemplate().delete(image);
		
	}
	
	public List<Image> findImageListDelByArti(Article arti){
		
		List<Image> imgList=this.getHibernateTemplate().find("from Image as i where i.arti=? and i.deleteFlag=true",arti);
		if(imgList!=null&&imgList.size()>=1){
			return imgList;
		}else{
			return null;
		}
	}
	
	public List<Image> findImageListByArti(Article arti){
		
		List<Image> imgList=this.getHibernateTemplate().find("from Image as i where i.arti=? and i.deleteFlag=false",arti);
		if(imgList!=null&&imgList.size()>=1){
			return imgList;
		}else{
			return null;
		}
	}
	
	public List<Image> findImageAll() {
		
		List<Image> imgList=this.getHibernateTemplate().find("from Image as i where i.deleteFlag=false");
		if(imgList!=null&&imgList.size()>=1){
			return imgList;
		}else{
			return null;
		}
		
	}

	

	public Image findImageByName(String name) {
		
		List<Image> imgList= this.getHibernateTemplate().find("from Image as i where i.imgSaveName=?",name);
		if(imgList!=null&&imgList.size()>=1){
			Image image=imgList.get(0);
			return image;
		}else{
			return null;
		}
	}

	public Image findImageByImgId(Integer imgId) {
		
		Image image=this.getHibernateTemplate().get(Image.class, imgId);
		if(image!=null){
			return image;
		}else{
			
			return null;
		}
		
	}

	public void save(Image image) {
		
		this.getHibernateTemplate().save(image);
	}

	public void update(Image image) {
		
		this.getHibernateTemplate().update(image);
		
	}

	/* 
	 * {见接口方法说明}
	 */
	public List<Image> findImageListByNav(Navigation nav) {
		
		List<Image> imageListByNav = this.getHibernateTemplate().find("from Image as i where i.nav=?",nav);
		if(imageListByNav!=null&&imageListByNav.size()>=1){
			return imageListByNav;
		}else{
			
			return null;
		}
	}

	/* 
	 * {见接口方法说明}
	 */
	public List<Image> findImageAll(int pageNo, int pageSize) {
		
		if(pageNo<1){
			return null;
		}
		int offset=(pageNo-1)*pageSize;
		List<Image> imageList = this.findByPage("from Image as i where i.deleteFlag=false", offset, pageSize);
		if(imageList!=null&&imageList.size()>=1){
			return imageList;
		}else{
			logger.debug(this.getClass().getMethods().toString()+"为空");
			return null;
		}
	}

	/* 
	 * {见接口方法说明}
	 */
	public int getImageCount() {
		
		if(this.findImageAll()!=null&&this.findImageAll().size()>=1){
			return this.findImageAll().size();
		}else{
			return 0;
		}
		
	}
	

}
